package com.tzpay.tzpay;

import android.app.Application;
import android.content.Context;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.model.GlideUrl;
import com.tzpay.tzpay.crash.CrashHandler;
import com.tzpay.tzpay.integration.okhttp3.OkHttpUrlLoader;

import java.io.InputStream;

import retrofit2.Retrofit2Instance;

public class BaseApplication extends Application {
    private static Context applicationContext;

    @Override
    public void onCreate() {
        // 自我强制执行的严格模式，可超级严格的规范app,问题是容易导致闪退。。
        // if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
        // StrictMode.setThreadPolicy(new
        // StrictMode.ThreadPolicy.Builder().detectAll().penaltyDialog().build());
        // StrictMode.setVmPolicy(new
        // StrictMode.VmPolicy.Builder().detectAll().penaltyDeath().build());
        // }

        super.onCreate();
        applicationContext = this.getApplicationContext();

        CrashHandler crashHandler = CrashHandler.getInstance(); // App崩溃记录
        crashHandler.init(this); // 传入参数必须为Activity，否则AlertDialog将不显示。

        Retrofit2Instance instance = Retrofit2Instance.init(applicationContext);
        Glide.get(this)
                .register(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory(instance.getOkHttpClient()));
    }

    // app退出时调用
    @Override
    public void onTerminate() {
        super.onTerminate();
//        try {
//            BaseHttpUtil.getClient().cancelAllRequests(true);// 关闭http通讯
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
    }

    public static Context getContext() {
        return applicationContext;
    }

}
